//
//  RankViewController.m
//  XiangCaoJiaYuan
//
//  Created by tongj on 2017/8/23.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "RankViewController.h"
#import "UIButton+LT.h"
#import "RankCell.h"
@interface RankViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView* image;
    UIView* Dview;
    UITableView* _tableView;
    NSMutableArray* DataSource;
    UIButton* _selecBut;
}
@property (nonatomic,strong)NSMutableArray *dataArray1;
@end

@implementation RankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"排行榜";
    _dataArray1  = [[NSMutableArray alloc]init];

    [UIButton backButton:self.navigationItem Col:self];
    
    image = [[UIImageView alloc]init];
    image.frame = CGRectMake(0, 0,UIScreen_WIDTH, UIScreen_HEIGHT-64);
    image.image = [UIImage imageNamed:@"排行版08-21_02"];
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    [self createUI];
    [self questData:@"1"];

    // Do any additional setup after loading the view.
}
-(void)questData:(NSString *)type{
    [_dataArray1 removeAllObjects];
    NSString *url = [NSString stringWithFormat:@"%@campaign/getRankingList?type=%@",ServiceURL2,type];
    [YBRequestManager GET:url finished:^(NSData *data) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [NSMutableArray array];
        NSArray *str11 = dict[@"data"];
        for (NSDictionary *dict1 in str11)
        {
            [array addObject:dict1];
            NSLog(@"%@",dict1);

//            RankModel *model = [[RankModel alloc]initWithDictionary:dict1 error:nil];
//            [_dataArray1 addObject:model];
        }
  
        NSArray *array2 = [RankModel arrayOfModelsFromDictionaries:array error:nil];
      
        [_dataArray1 addObjectsFromArray:array2];
        [_tableView reloadData];
 
        
    } failed:^(NSString *error) {
        
    }];
    
}
#pragma mark 创建视图
-(void)createUI{
    
    Dview = [[UIView alloc]initWithFrame:CGRectMake(41*WIDTH, 145*HEIGHT,(UIScreen_WIDTH-44*WIDTH*2), UIScreen_HEIGHT-150*HEIGHT-54*HEIGHT-64)];
    Dview.backgroundColor = [UIColor redColor];
    [image addSubview:Dview];
    
    UIView* ButView = [[UIView alloc]initWithFrame:CGRectMake(0,0,Dview.frame.size.width, 56*HEIGHT)];
    ButView.backgroundColor = [UIColor whiteColor];
    [Dview addSubview:ButView];
    
    NSArray* arr = @[@"红包总额",@"红包总数",@"奖品总数"];

    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
    //设置frame
    segment.frame = CGRectMake(0, 5, Dview.frame.size.width,  46*HEIGHT);
    segment.selectedSegmentIndex = 0;
    segment.tintColor =  HEXRGB(0xd2e8e5);
    [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [Dview addSubview:segment];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, ButView.frame.size.height, Dview.frame.size.width, Dview.frame.size.height-ButView.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    [Dview addSubview:_tableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld",_dataArray1.count);

    return _dataArray1.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* str = @"RankCell";
    RankCell* cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        
         cell = [[[NSBundle mainBundle] loadNibNamed:@"RankCell" owner:self options:nil] firstObject];
    }
    RankModel *model = _dataArray1[indexPath.row];
    cell.rankk.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    [cell setModel:model];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(void)change:(UISegmentedControl *)sender
{
    NSString *type = [NSString stringWithFormat:@"%ld",sender.selectedSegmentIndex+1];
    [self questData:type];
    
}
-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
